package com.atguigu.guli.service.edu.controller.api;

import com.atguigu.guli.common.base.result.R;
import com.atguigu.guli.common.base.util.JwtInfo;
import com.atguigu.guli.common.base.util.JwtUtils;
import com.atguigu.guli.service.edu.entity.CourseCollect;
import com.atguigu.guli.service.edu.entity.vo.CourseCollectVo;
import com.atguigu.guli.service.edu.service.CourseCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lj on 2021/2/15.
 * @version 1.0
 */
//@CrossOrigin
@RestController
@RequestMapping("/api/edu/course-collect")
@Slf4j
public class ApiCourseCollectController {
    @Autowired
    private CourseCollectService courseCollectService;
    @ApiOperation(value = "判断是否收藏")
    @GetMapping("auth/is-collect/{courseId}")
    public R isCollect(
    @ApiParam(name = "courseId", value = "课程id", required = true)
        @PathVariable String courseId,
        HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        final List<CourseCollect> courseCollects = courseCollectService.list(new QueryWrapper<CourseCollect>()
                .eq("course_id", courseId).eq("member_id", jwtInfo.getId()));
        return R.ok().data("isCollect", !CollectionUtils.isEmpty(courseCollects));
    }

    @ApiOperation(value = "收藏课程")
    @PostMapping("auth/save/{courseId}")
    public R save(
    @ApiParam(name = "courseId", value = "课程id", required = true)
        @PathVariable String courseId,
    HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        //先判断是否已经收藏了
        final List<CourseCollect> courseCollects = courseCollectService.list(new QueryWrapper<CourseCollect>()
                .eq("course_id", courseId).eq("member_id", jwtInfo.getId()));
        if(CollectionUtils.isEmpty(courseCollects)){
            courseCollectService.save(new CourseCollect(courseId,jwtInfo.getId()));
        }
        return R.ok();
    }

    @ApiOperation(value = "获取课程收藏列表")
    @GetMapping("auth/list")
    public R collectList(HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        List<CourseCollectVo> list = courseCollectService.selectListByMemberId(jwtInfo.getId());
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "取消收藏课程")
    @DeleteMapping("auth/remove/{courseId}")
    public R remove(
    @ApiParam(name = "courseId", value = "课程id", required = true)
    @PathVariable String courseId,
    HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        boolean result = courseCollectService.remove(new QueryWrapper<CourseCollect>().eq("course_id",courseId).eq("member_id",jwtInfo.getId()));
        if (result) {
            return R.ok().message("已取消");
        } else {
            return R.error().message("取消失败");
        }
    }
}
